﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using E4RPTools;
using RestaurantCommon;

namespace Restaurant.Controls
{
    public partial class TableXtraForm : XtraForm
    {
        private long _tableId;
        private Controller _controller;
        private MesaDataSet _table;

        #region Properties

        public long TableId
        {
            set { _tableId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public TableXtraForm()
        {
            InitializeComponent();
            Load += TableXtraForm_Load;
            nameTextEdit.TextChanged += nameTextEdit_TextChanged;
            backColorEdit.TextChanged += backColorEdit_TextChanged;
            nameColorEdit.TextChanged += nameColorEdit_TextChanged;
            oKSimpleButton.Click += oKSimpleButton_Click;
        }

        #region Private Methods

        private static string GetColorName(Color color)
        {
            var colorConv = new ColorConverter();
            return colorConv.ConvertToInvariantString(color);
        }

        private void SetTableData()
        {
            _table = new MesaDataSet();
            if (_tableId <= 0)
            {
                var table = _table.Mesa.NewMesaRow();
                table.Id = _tableId;
                table.Nome = "";
                table.NumeroLugares = 0;
                table.CorFundo = GetColorName(SystemColors.Control);
                table.CorNome = GetColorName(Color.Black);
                _table.Mesa.AddMesaRow(table);
            }
            else
                _table.Merge(_controller.GetTable(_tableId));            
        }

        private void GetDataFromControls()
        {
            _table.Mesa[0].Nome = nameTextEdit.Text;
            _table.Mesa[0].NumeroLugares = Convert.ToInt32(placesCalcEdit.Value);
            _table.Mesa[0].CorFundo = GetColorName(backColorEdit.Color);
            _table.Mesa[0].CorNome = GetColorName(nameColorEdit.Color);
        }

        private void SetDataToControls()
        {
            nameTextEdit.Text = _table.Mesa[0].Nome;
            placesCalcEdit.EditValue = _table.Mesa[0].NumeroLugares;
            backColorEdit.Color = GetColor(_table.Mesa[0].CorFundo);
            nameColorEdit.Color = GetColor(_table.Mesa[0].CorNome);
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(nameTextEdit, "");
            dxErrorProvider.SetError(placesCalcEdit, "");
            dxErrorProvider.SetError(backColorEdit, "");
            dxErrorProvider.SetError(nameColorEdit, "");
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Nome":
                    return nameTextEdit;
                case "NumeroLugares":
                    return placesCalcEdit;
                case "CorFundo":
                    return backColorEdit;
                case "CorNome":
                    return nameColorEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (Error error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        private static Color GetColor(string colorName)
        {
            var colorConv = new ColorConverter();
            return (Color)colorConv.ConvertFromInvariantString(colorName);
        }
        

        #endregion Private Methods

        void TableXtraForm_Load(object sender, EventArgs e)
        {
            SetTableData();
            SetDataToControls();
        }

        void nameTextEdit_TextChanged(object sender, EventArgs e)
        {
            singleTableUserControl.TableName = nameTextEdit.Text;
        }

        void nameColorEdit_TextChanged(object sender, EventArgs e)
        {
            var colorConv = new ColorConverter();
            singleTableUserControl.TableNameColor = colorConv.ConvertToInvariantString(nameColorEdit.Color);
        }

        void backColorEdit_TextChanged(object sender, EventArgs e)
        {
            var colorConv = new ColorConverter();
            singleTableUserControl.TableBackColor = colorConv.ConvertToInvariantString(backColorEdit.Color);
        }
        
        void oKSimpleButton_Click(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            Result result = _controller.IsTableValid(_table.Mesa[0]);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveTable(_table.Mesa[0]);
        }
    }
}
